<template>
    <div>
        <el-card class="box-card" shadow="never">
            <!--导航栏-->
            <el-page-header @back="back" :content="content">
            </el-page-header>
            <el-form ref="form" class="edit-el-form"  :model="form" :rules="rules" label-width="auto">
                <el-form-item label="网格名称" prop="g_name">
                    <el-input v-model="form.g_name" required></el-input>
                </el-form-item>
                <el-form-item label="所属社区" prop="c_id">
                   <el-select v-model="form.c_id" placeholder="请选择社区">
                       <el-option
                               v-for="item in allcommunitylist"
                               :key="item.value"
                               :label="item.label"
                               :value="item.value">
                       </el-option>
                   </el-select>
                </el-form-item>
                <el-form-item label="网格图片" prop="pic">
                    <Upload :limit="1" :file_list="pic_list" :catch_urls="catch_urls" :remove_urls="remove_urls" :upload_tip="upload_tip"></Upload>
                </el-form-item>
                <el-form-item label="占地面积" prop="cover_area">
                   <el-input v-model="form.cover_area">
                     <template slot="append">平方公里</template>
                   </el-input>
                </el-form-item>
                <el-form-item label="常住人口" prop="population">
                   <el-input v-model="form.population">
                       <template slot="append">万人</template>
                   </el-input>
                </el-form-item>
                <el-form-item label="业务电话" prop="phone">
                   <el-input v-model="form.phone" required></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model.number="form.sort" required>
                      <template slot="append">注：排序越大越靠前，默认100</template>
                    </el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-radio-group v-model="form.enabled">
                        <el-radio border label="0">禁用</el-radio>
                        <el-radio border label="1">正常</el-radio>
                    </el-radio-group>
                </el-form-item>

                <!--查看不展示按钮-->
                <el-form-item v-if="edit_type!=0">
                    <el-button type="primary" @click="submitForm('form')" :loading="loading">{{button_name}}</el-button>
                    <el-button @click="back">取消</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script>
    import { getGridInfo,editGrid } from '@/api/grid.js';
    import { getAllCommunityList } from '@/api/community.js';
    import Upload from '@/components/Upload.vue';
    export default {
        name:'GridEdit',
        components:{Upload},
        data() {
            return {
                form: {
                    g_name: '',
                    c_id:'',
                    cover_area:'',
                    population:'',
                    pic:'',
                    phone:'',
                    sort: 100,
                    enabled: '1',
                },
                //校验规则
                rules:{
                    g_name: [
                        { required:true , message:'请输入网格名称',trigger:'blur' }
                    ],
                    c_id:[
                        { required:true , message:'请选择所属社区',trigger:'blur' }
                    ],
                    sort:[
                        { required:true , message:'请输入排序号',trigger:'blur' },
                        { type:'number' , message:'排序号必须为数字' }
                    ]
                },
                allcommunitylist:[],
                //加载组件
                loading: false,
                button_name:'立即提交',
                content:'网格添加',
                pic_list:[],
                image_urls:[],
                upload_tip:'只能上传jpg/png/gif文件，且不超过500kb，仅允许上传一张'
            }
        },
        methods: {
            submitForm(formName){
                let that = this;
                    this.$refs[formName].validate((valid) => {
                            if (valid) {
                                this.loading=true;
                                this.button_name='正在提交';
                                var send_data={
                                    api_token:this.$store.getters.getSessionId,
                                    g_name:this.form.g_name,
                                    c_id:this.form.c_id,
                                    pic:this.form.pic,
                                    population:this.form.population,
                                    cover_area:this.form.cover_area,
                                    phone:this.form.phone,
                                    sort:this.form.sort,
                                    enabled:this.form.enabled,
                                };
                                if(this.id!=-1){
                                    send_data.id=this.id;
                                }
                                editGrid(send_data).then(function(response){
                                     let code=response.data.code;
                                     let type=code==1?'success':'error';
                                     let msg=code==1?'成功':'失败';
                                     that.$message({
                                         message:that.content+msg,
                                         duration:1200,
                                         type:type,
                                         onClose:function(e){
                                             that.loading=false;
                                             if(code==1){
                                                that.$router.push('/gridlist');
                                             }else{
                                                that.button_name='立即提交';
                                             }
                                         }
                                    })
                                })
                            }else{
                                setTimeout(()=>{
                                  const is_error=document.getElementsByClassName('is-error');
                                  is_error[0].querySelector('input').focus();
                                },100);
                                return false;
                            }
                    })
            },
            back(){
                this.$router.go(-1);
            },
            remove_urls(remove_index){
              this.image_urls.splice(remove_index,1);
            },
            catch_urls(url){
               this.image_urls.push(url);
            },
            getGridInfo(){
               if(this.id!=-1){
                   let send_data={
                       api_token:this.$store.getters.getSessionId,
                       id:this.id
                   }
                   let that=this;
                   getGridInfo(send_data).then(function(response){
                       if(response.data.code==1){
                         let val=response.data.info;
                         that.form.g_name=val.g_name;
                         that.form.c_id=val.c_id;
                         that.form.pic=val.pic;
                         that.image_urls=[val.pic];
                         that.form.population=val.population==0?'':val.population;
                         that.form.cover_area=val.cover_area==0?'':val.cover_area;
                         that.form.phone=val.phone;
                         that.form.sort=val.sort;
                         that.form.enabled=val.enabled.toString();
                         that.pic_list.push({
                             name:0,
                             url:val.pic
                         });
                       }
                   })
               }
            }
        },
        created(){
            let send_data={
               api_token:this.$store.getters.getSessionId
            };
            let that=this;
            getAllCommunityList(send_data).then(function(response){
               that.allcommunitylist=response.data.list;
               that.getGridInfo();
            })
            //修改标题
            if(this.edit_type==1){
                this.content='网格添加';
            }else if(this.edit_type==0){
                this.content='网格详情';
            }else if(this.edit_type==2){
                this.content='网格编辑';
            }
        },
        computed:{
            id(){
                return this.$route.params.id;
            },
            edit_type(){
                return this.$route.params.edit_type;
            },
        }
    }
</script>

<style>
    .edit-el-form{
        margin-top:30px;
    }
</style>
